[#ftl]
[#include "commonDef.ftl" /]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
[#if table_own?? ]
<!-- ${table_own.tableTitle} -->
<mapper namespace="${pack2_dao}">
[#if table_own.sqlInterfaces?? && table_own.sqlInterfaces?size>0]
    <sql id="_select">
        [#list table_own.ownEntity as b]
        ${table_own.tableAlias}.${b.columnCode}[#if b_has_next],[/#if]
        [/#list]
    </sql>
    <sql id="_where">
        [@whereSqlHandle tx=table_own tblAlias=table_own.tableAlias+"." colAlias="" blankflag="  " /]
        [@versionTokenHandle entitys=table_own.generalEntity colAlias="" blankflag="  " /]
    </sql>
[#list table_own.sqlInterfaces as tx]
    [#if tx.needFlag == '1']
    <!-- ${tx.sqlTitle +' '+ tx.sqlDesc} -->
    [#if tx.sqlGenerateType == '1' || tx.sqlGenerateType == '2']
    [#if tx.sqlCode == 'pageList'][#-- 分页查询集合数据--]
    <sql id="_wherePage">
        [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
        [#list tx.whereData as b]
        <if test="${testConditionFunc(b,b.columnJavaCode)}" >and ${b.tableAlias}.${b.columnCode} ${conditionSymbolFunc("",b)}</if>
        [/#list]
        [#else]
        [#list table_own.ownEntity as b]
        [#if !checkTypeFunc(b.javaType,dateJavaTypes)]
        <if test="${testConditionFunc(b,b.columnJavaCode)}" >and ${table_own.tableAlias}.${b.columnCode} ${conditionSymbolFunc("",b)}</if>
        [/#if]
        [/#list]
        [/#if]
    </sql>
    <select id="${tx.sqlId}" resultType="${pack2_entityVo}" parameterType="${pack2_entityVo}">
        select
          [#if tx.sqlGenerateType == '2' && tx.entityData?? && tx.entityData?size>0]
          [#list tx.entityData as b]
          ${b.tableAlias}.${b.columnCode} as ${b.columnJavaCode}[#if b_has_next],[/#if]
          [/#list]
          [#else]
          <include refid="_select"/>
          [/#if]
        [#if tx.sqlGenerateType == '2' && (tx.tableData)?length>0]
        ${tx.tableData}
        [#else]
        from ${table_own.tableCode} ${table_own.tableAlias}
        [/#if]
        <where>
          <include refid="_wherePage"/>
        </where>
    </select>
    [#elseif tx.sqlCode == 'selectList'][#-- 查询集合数据--]
    <select id="${tx.sqlId}" resultType="${pack2_entity}" parameterType="${pack2_entityVo}">
        [@selectSqlHandle tx=tx whereStr='_where' blankflag="  " /]
    </select>
    
    [#elseif tx.sqlCode == 'selectOne'][#-- 查询单条数据--]
    <select id="${tx.sqlId}" resultType="${pack2_entity}" parameterType="${pack2_entityVo}">
        [@selectSqlHandle tx=tx whereStr='_where' blankflag="  " /]
    </select>
    
    [#elseif tx.sqlCode == 'selectListEx'][#-- 查询集合数据--]
    <select id="${tx.sqlId}" resultType="${pack2_entityVo}" parameterType="${pack2_entityVo}">
        [@selectSqlHandle tx=tx whereStr='_where' blankflag="  " /]
    </select>
    
    [#elseif tx.sqlCode == 'selectOneEx'][#-- 查询单条数据--]
    <select id="${tx.sqlId}" resultType="${pack2_entityVo}" parameterType="${pack2_entityVo}">
        [@selectSqlHandle tx=tx whereStr='_where' blankflag="  " /]
    </select>
    
    [#elseif tx.sqlCode == 'countMap'][#-- 查询总数--]
    <select id="${tx.sqlId}" resultType="java.lang.Integer" parameterType="${pack2_entityVo}">
       select 
         count(*)
       [#if tx.sqlGenerateType == '2' && (tx.tableData)?length>0]
       ${tx.tableData}
       [#else]
       from ${table_own.tableCode} ${table_own.tableAlias}
       [/#if]
       <where>
          [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
          [@whereSqlHandle tx=tx tblAlias=tx.tableAlias+"." blankflag="" /]
          [#else]
          <include refid="_where"/>
          [/#if]
       </where>
    </select>
    
    [#elseif tx.sqlCode == 'updateMap'][#-- 更新--]
    <update id="${tx.sqlId}">
      update ${table_own.tableCode} 
      <set>
      [#list table_own.generalEntity as b]
        <if test="vo.${b.columnJavaCode} != null[#if isJavaTypeFunc(b,strJavaTypes)] and vo.${b.columnJavaCode} != ''[/#if]" >${b.columnCode} = ${'#{'+'vo.'+b.columnJavaCode+'}'},</if>
        [#if checkTypeFunc(b.columnJavaCode,["version","versionNum"])]
        <if test="vo.${b.columnJavaCode} == null" >${b.columnCode} = ${b.columnCode}+1,</if>
        [/#if]
      [/#list]
      </set>
      <where>
        [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
        [@whereSqlHandle tx=tx tblAlias="" colAlias="map." blankflag="    " /]
        [#else]
        [@whereSqlHandle tx=table_own tblAlias="" colAlias="map." blankflag="    " /]
        [/#if]
        [@versionTokenHandle entitys=table_own.generalEntity colAlias="map." blankflag="    " /]
      </where> 
    </update>
    
    [#elseif tx.sqlCode == 'deleteMap'][#-- 删除数据--]
    <delete id="${tx.sqlId}" parameterType="${pack2_entityVo}">
      delete from ${table_own.tableCode}
      <where>
        [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
        [@whereSqlHandle tx=tx tblAlias="" colAlias="" blankflag="    " /]
        [#else]
        [@whereSqlHandle tx=table_own tblAlias="" colAlias="" blankflag="    " /]
        [/#if]
        [@versionTokenHandle entitys=table_own.generalEntity  colAlias="" blankflag="    " /]
      </where>
    </delete>
    
    [#elseif tx.sqlCode == 'insert'][#-- 插入单条数据--]
    <insert id="${tx.sqlId}" parameterType="${pack2_entity}"${keyGeneratedFunc(table_own)}>
      insert into ${table_own.tableCode} 
      <trim prefix="(" suffix=")" suffixOverrides="," >
        [#list table_own.ownEntity as b]
        <if test="${b.columnJavaCode} != null" >${b.columnCode},</if>
        [/#list]
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides="," >
        [#list table_own.ownEntity as b]
        <if test="${b.columnJavaCode} != null" >${'#{'+b.columnJavaCode+'}'},</if>
        [/#list]
      </trim>
    </insert>
    
    [#else]
    [#if tx.sqlType?length>0]
    [#if tx.sqlType =="select"]
    <select id="${tx.sqlId}" ${(tx.returnTypeXml?length>0)?then(tx.returnTypeXml,'resultType="'+pack2_entityVo+'"')} ${(tx.parameterTypeXml?length>0)?then(tx.parameterTypeXml,'parameterType="java.util.Map"')}>
    ${tx.tableData}
    </select>
    
    [#elseif tx.sqlType =="update"]
    <update id="${tx.sqlId}" ${(tx.parameterTypeXml?length>0)?then(tx.parameterTypeXml,'parameterType="'+pack2_entity+'"')}>
    ${tx.tableData}
    </update>
    
    [#elseif tx.sqlType =="insert"]
    <insert id="${tx.sqlId}" ${(tx.parameterTypeXml?length>0)?then(tx.parameterTypeXml,'parameterType="'+pack2_entity+'"')}>
    ${tx.tableData}
    </insert>
    
    [#elseif tx.sqlType =="delete"]
    <delete id="${tx.sqlId}" ${(tx.parameterTypeXml?length>0)?then(tx.parameterTypeXml,'parameterType="java.util.Map"')}>
    ${tx.tableData}
    </delete>
    
    [/#if]
    
    [#else]
    ${tx.tableData}
    [/#if]
    
    [/#if]
    [#elseif tx.sqlGenerateType == '4']
    [#if tx.sqlType =="select"]
    <select id="${tx.sqlId}" resultType="${(tx.returnTypeXml?length>0)?then(tx.returnTypeXml,pack2_entityVo)}" parameterType="${(tx.parameterTypeXml?length>0)?then(tx.parameterTypeXml,'java.util.Map')}">
    select
        [#list tx.entityData as b]
        ${b.tableAlias}.${b.columnCode} as ${b.columnJavaCode}[#if b_has_next],[/#if]
        [/#list]
    [#if !(tx.tableData?upper_case?contains('FROM'))]
    from ${table_own.tableCode} ${table_own.tableAlias}
    [/#if]
    ${tx.tableData}
    </select>
    
    [#elseif tx.sqlType =="update"]
    <update id="${tx.sqlId}" parameterType="${(tx.parameterTypeXml?length>0)?then(tx.parameterTypeXml,pack2_entity)}">
    </update>
    
    [#elseif tx.sqlType =="insert"]
    <insert id="${tx.sqlId}" parameterType="${(tx.parameterTypeXml?length>0)?then(tx.parameterTypeXml,pack2_entity)}">
    </insert>
    
    [#elseif tx.sqlType =="delete"]
    <delete id="${tx.sqlId}" parameterType="${(tx.parameterTypeXml?length>0)?then(tx.parameterTypeXml,'java.util.Map')}">
    </delete>
    
    [/#if]
    
    [#elseif tx.sqlGenerateType == '3']
    ${tx.tableData}
    
    [/#if]
    [/#if]
[/#list]

[#else]
    <sql id="_select">
         [#list table_own.ownEntity as b]
         ${table_own.tableAlias}.${b.columnCode}[#if b_has_next],[/#if]
         [/#list]
    </sql>
    <sql id="_where">
        [@whereSqlHandle tx=table_own tblAlias=table_own.tableAlias+"." colAlias="" blankflag="" /]
    </sql>
    <sql id="_wherePage">
         [#list table_own.ownEntity as b]
         [#if !checkTypeFunc(b.javaType,dateJavaTypes)]
         <if test="${testConditionFunc(b,b.columnJavaCode)}" >and ${table_own.tableAlias}.${b.columnCode} ${conditionSymbolFunc("",b)}</if>
         [/#if]
         [/#list]
    </sql>
    
    <!-- 查询分页数据-->
    <select id="selectPageList" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode} ${table_own.tableAlias}
        <where>
          <include refid="_wherePage"/>
        </where>
    </select>
    
    <!-- 查询集合数据-->
    <select id="selectList" resultType="${pack2_entity}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode} ${table_own.tableAlias}
        <where>
          <include refid="_where"/>
        </where>
    </select>
    
    <!-- 查询单条数据-->
    <select id="selectOne" resultType="${pack2_entity}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode} ${table_own.tableAlias}
        <where>
          <include refid="_where"/>
        </where>
    </select>
    
    <!-- 查询集合数据-->
    <select id="selectListEx" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode} ${table_own.tableAlias}
        <where>
          <include refid="_where"/>
        </where>
    </select>
    
    <!-- 查询单条数据-->
    <select id="selectOneEx" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode} ${table_own.tableAlias}
        <where>
          <include refid="_where"/>
        </where>
    </select>
    
    <!-- 查询总数-->
    <select id="countByMap" resultType="java.lang.Integer" parameterType="java.util.Map">
         select 
             count(*)
         from ${table_own.tableCode} ${table_own.tableAlias}
         <where>
           <include refid="_where"/>
         </where>
    </select>
    
    <!-- 更新-->
    <update id="updateByMap">
        update ${table_own.tableCode} 
        <set>
          [#list table_own.generalEntity as b]
          <if test="vo.${b.columnJavaCode} != null[#if isJavaTypeFunc(b,strJavaTypes)] and vo.${b.columnJavaCode} != ''[/#if]" >${b.columnCode} = ${'#{'+'vo.'+b.columnJavaCode+'}'},</if>
          [#if checkTypeFunc(b.columnJavaCode,["version","versionNum"])]
          <if test="vo.${b.columnJavaCode} == null" >${b.columnCode} = ${b.columnCode}+1,</if>
          [/#if]
          [/#list]
        </set>
        <where>
          [@whereSqlHandle tx=table_own tblAlias="" colAlias="map." blankflag="    " /]
          [@versionTokenHandle entitys=table_own.generalEntity  colAlias="map." blankflag="    " /]
        </where> 
    </update>
    
    <!-- 删除数据-->
    <delete id="deleteByMap" parameterType="java.util.Map">
        delete from ${table_own.tableCode}
        <where>
          [@whereSqlHandle tx=table_own tblAlias="" colAlias="" blankflag="    " /]
          [@versionTokenHandle entitys=table_own.generalEntity colAlias="" blankflag="    " /]
        </where>
    </delete>
    
    <!-- 插入单条数据-->
    <insert id="insert" parameterType="${pack2_entity}"${keyGeneratedFunc(table_own)}>
        insert into ${table_own.tableCode} 
        <trim prefix="(" suffix=")" suffixOverrides="," >
          [#list table_own.ownEntity as b]
          <if test="${b.columnJavaCode} != null" >${b.columnCode},</if>
          [/#list]
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          [#list table_own.ownEntity as b]
          <if test="${b.columnJavaCode} != null" >${'#{'+b.columnJavaCode+'}'},</if>
          [/#list]
        </trim>
    </insert>
[/#if]

</mapper>
[/#if]
[#-- 查询SQL处理 --]
    [#macro selectSqlHandle tx whereStr blankflag]
      select
        [#if tx.sqlGenerateType == '2' && tx.entityData?? && tx.entityData?size>0]
        [#list tx.entityData as b]
        ${b.tableAlias}.${b.columnCode} as ${b.columnJavaCode}[#if b_has_next],[/#if]
        [/#list]
        [#else]
        <include refid="_select"/>
        [/#if]
      [#if tx.sqlGenerateType == '2' && (tx.tableData)?length>0]
      ${tx.tableData}
      [#else]
      from ${table_own.tableCode} ${table_own.tableAlias}
      [/#if]
      <where>
        [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
        [@whereSqlHandle tx=tx tblAlias=tx.tableAlias+"." colAlias="" blankflag=blankflag /]
        [#else]
        <include refid="${whereStr}"/>
        [/#if]
      </where>
    [/#macro]
[#-- SQL条件处理 --]
    [#macro whereSqlHandle tx tblAlias colAlias blankflag]
    ${blankflag}<!-- 默认必有条件 -->
    [#if tx.chooseWhereData?size>1]
    ${blankflag}<choose>
      [#list tx.chooseWhereData as tb]
      [#if tb_index<(tx.chooseWhereData?size-1)]
      [#if tb?size>1]
      ${blankflag}<when test="[#list tb as b]${colAlias+b.columnJavaCode} != null[#if b_has_next] or [/#if][/#list]">
      [@chooseSqlHandle tb=tb tblAlias=tblAlias colAlias=colAlias blankflag=blankflag+"    " /]
      ${blankflag}</when>
      [#else]
      ${blankflag}<when test="${colAlias+b.columnJavaCode} != null">[#list tb as b] and ${tblAlias+b.columnCode} ${conditionSymbolFunc(colAlias,b)}[/#list]</when>
      [/#if]
      [#else]
      [#if tb?size>1]
      ${blankflag}<otherwise>
      [@chooseSqlHandle tb=tb tblAlias=tblAlias colAlias=colAlias blankflag=blankflag+"    " /]
      ${blankflag}</otherwise>
      [#else]
      ${blankflag}<otherwise>[#list tb as b]and ${tblAlias+b.columnCode} ${conditionSymbolFunc(colAlias,b)} [/#list]</otherwise>
      [/#if]
      [/#if]
      [/#list]
    ${blankflag}</choose>
    [#elseif tx.chooseWhereData?size==1][#assign tb=tx.chooseWhereData[0]]
    [#if tb?size>1]
      [@chooseSqlHandle tb=tb tblAlias=tblAlias colAlias=colAlias blankflag=blankflag+"" /]
    [#else]
    [#list tb as b]
    ${blankflag}and ${tblAlias+b.columnCode} ${conditionSymbolFunc(colAlias,b)}
    [/#list]
    [/#if]
    [/#if]
    [#if tx.mustWhereData?? && tx.mustWhereData?size>0]
    [#list tx.mustWhereData as b]
    ${blankflag}and ${tblAlias+b.columnCode} ${conditionSymbolFunc(colAlias,b)}
    [/#list]
    [/#if]
    [#if tx.emptyWhereData?? && tx.emptyWhereData?size>0]
    [#list tx.emptyWhereData as b]
    ${blankflag}<if test="${colAlias+b.columnJavaCode} != null" >and ${tblAlias+b.columnCode} ${conditionSymbolFunc(colAlias,b)}</if>
    [/#list]
    [/#if]
    [/#macro]
[#-- if/else条件处理 --]
    [#macro chooseSqlHandle tb tblAlias colAlias blankflag]
    [#list tb as b]
    [#if b_index%2!=0]
    ${blankflag}<choose>
      ${blankflag}<when test="${colAlias+tb[b_index-1].columnJavaCode} != null">and ${tblAlias+tb[b_index-1].columnCode} ${conditionSymbolFunc(colAlias,tb[b_index-1])}</when>
      ${blankflag}<otherwise>and ${tblAlias+b.columnCode} ${conditionSymbolFunc(colAlias,b)}</otherwise>
    ${blankflag}</choose>
    [/#if]
    [/#list]
    [/#macro]
[#-- 运算符处理 --]
    [#function conditionSymbolFunc colAlias,b]
    [#if b.conditionSymbol == 'in']
      [#return 'in(<foreach collection="'+colAlias+b.columnJavaCode+'" item="idx" separator=",">#\{idx}</foreach>)']
    [#else]
      [#return conditionSymbolMap[b.conditionSymbol] +' #{'+colAlias+b.columnJavaCode+'}']
    [/#if]
    [/#function]
[#-- version/token/dataStatus --]
    [#macro versionTokenHandle entitys  colAlias blankflag]
    [#list entitys as b]
    [#if checkTypeFunc(b.columnJavaCode,versionLists) ]
    ${blankflag}<if test="${colAlias+b.columnJavaCode} != null" >and ${b.columnCode} = ${'#{'+colAlias+b.columnJavaCode+'}'}</if>
    [/#if]
    [/#list]
    [/#macro]
[#-- 返回主键字段 --]
    [#function keyGeneratedFunc tb]
    [#if tb.keyGenerated()??]
    [#return ' keyProperty="'+tb.keyGenerated().columnJavaCode+'"']
    [/#if]
    [#return ""]
    [/#function]
[#-- test比较条件 --]
    [#function testConditionFunc b columnJavaCode]
    [#if isJavaTypeFunc(b,strJavaTypes)]
    [#return columnJavaCode+"!=null and "+columnJavaCode+"!=''"]
    [/#if]
    [#return columnJavaCode+"!=null"]
    [/#function]